﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using System;
using System.Windows.Forms;

namespace ComponentGIS.View
{
    public partial class FormAddField : Form
    {
        #region 字段属性
        IFeatureLayer m_pFeaLyr;
        esriFieldType m_pFieldType;
        #endregion

        #region 构造函数
        public FormAddField(IFeatureLayer pFeaLyr)
        {
            InitializeComponent();
            m_pFeaLyr = pFeaLyr;
            txtTargetLyr.Text = pFeaLyr.Name;
            cboFieldType.SelectedIndex = 0;
        }

        #endregion

        private void btnOK_Click(object sender, EventArgs e)
        {
            //1 验证不通过：字段名为空
            if (txtFieldName.Text == string.Empty)
            {
                MessageBox.Show("字段名不能为空！", "提示");
                this.DialogResult = DialogResult.None;
                txtFieldName.Focus();
                return;
            }
            //2 验证不通过：字段已存在
            int isExist = m_pFeaLyr.FeatureClass.FindField(txtFieldName.Text);
            if (isExist != -1)
            {
                MessageBox.Show("字段已存在，请重新修改字段名！", "提示");
                this.DialogResult = DialogResult.None;
                txtFieldName.Focus();
                return;
            }
            //3 验证通过
            IFieldEdit pFieldEdit = new FieldClass();
            pFieldEdit.Name_2 = txtFieldName.Text;
            pFieldEdit.AliasName_2 = txtFieldlAliasName.Text;
            pFieldEdit.Type_2 = m_pFieldType;
            IField pField = pFieldEdit;
            m_pFeaLyr.FeatureClass.AddField(pField);
        }

        private void cboFieldType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboFieldType.SelectedItem.ToString())
            {
                case "整型(Int)":
                    m_pFieldType = esriFieldType.esriFieldTypeInteger;
                    break;
                case "双精度(Double)":
                    m_pFieldType = esriFieldType.esriFieldTypeDouble;
                    break;
                case "字符串(String)":
                    m_pFieldType = esriFieldType.esriFieldTypeString;
                    break;
                default:
                    m_pFieldType = esriFieldType.esriFieldTypeString;
                    break;
            }
        }
    }
}
